// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
	const insertText = (val:any) => {
		const editor = vscode.window.activeTextEditor;
		const selection = editor?.selection;
		// 获取光标当前行
		const lineOfSelectedVar = selection?.active.line;
		// edit方法获取editBuilder实例，在后一行添加
		editor?.edit((editBuilder) => {
			// editBUilder在编辑页面时有3个方法insert、delete、replace
			// vscode.Position是一个坐标，传入行数和列数;
			// vscode.Range是由两个vscode.Position确定头尾后的一块区域
			editBuilder.insert(new vscode.Position(lineOfSelectedVar as number + 1, 0), val);
		});
	};
	// 注册命令名和对应的回调函数
	// registerCommand(command:string,callback:(args:any[])=>any,thisArg?:any):Disposable
	// 主要功能是给功能代码(callback)注册一个命令(command),然后通过subscriptions.push()给插件订阅对应的command事件
	const insertLog = vscode.commands.registerCommand('lhextension.insertLog', () => {
		// 拿到当前编辑页面的内容对象 editor
		const editor = vscode.window.activeTextEditor;
		// 拿到光标选中的文本并格式化
		const selection = editor?.selection;
		const text = editor?.document.getText(selection);
		// 在这里拼写console语句
		const logToInsert = `console.log('${text}:',${text});\n`;
		// 执行插行方法
		text ? insertText(logToInsert) : insertText('console.log();');
	});
	// 最后，我们把注册的命令时间放入上下文的订阅中
	context.subscriptions.push(insertLog);

	// Use the console to output diagnostic information (console.log) and errors (console.error)
	// This line of code will only be executed once when your extension is activated
	console.log('Congratulations, your extension "lhextension" is now active!');

	// The command has been defined in the package.json file
	// Now provide the implementation of the command with registerCommand
	// The commandId parameter must match the command field in package.json
	let disposable = vscode.commands.registerCommand('lhextension.hello', () => {
		// The code you place here will be executed every time your command is executed

		// Display a message box to the user
		vscode.window.showInformationMessage('Hello World from lhextension!');
	});

	context.subscriptions.push(disposable);
}

// this method is called when your extension is deactivated
export function deactivate() { }
